﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using DomusDotNet.Sbm.Client.SL4.Infrastructure;
using DomusDotNet.Sbm.Client.SL4.WCFServices;
using Moq;
using SharpTestsEx;
using System.Threading;
using Microsoft.Silverlight.Testing;
using DomusDotNet.Sbm.Client.SL4.Services;

namespace DomusDotNet.Sbm.Client.SL4.Tests
{
    [TestClass]
    public class the_WCFServiceCallResult : TestBase
    {
		Mock<ISharedBookshelfService> service;
		Mock<ISecurityContext> context;

		public the_WCFServiceCallResult() {
			service = new Mock<ISharedBookshelfService>();
			context = new Mock<ISecurityContext>();
			context.Setup(x => x.CurrentUser).Returns(DefaultPrincipal.Null);
		}
        [TestMethod]
        public void can_call_Login()
        {
            
            var request = new LoginRequest();

            var sut = CallService.For(request);
            sut.Service = service.Object;
			sut.SecurityContext = context.Object;


            sut.Execute(null);

            service.Verify(x => x.BeginLogin(request, It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }


        [TestMethod]
        public void can_call_Logout()
        {
            var request = new LogoutRequest();

            var sut = CallService.For(request);
            sut.Service = service.Object;
			sut.SecurityContext = context.Object;


            sut.Execute(null);

            service.Verify(x => x.BeginLogout(request, It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }

        [TestMethod]
        public void can_call_SearchBooks()
        {
            var request = new SearchBooksRequest();

            var sut = CallService.For(request);
            sut.Service = service.Object;
			sut.SecurityContext = context.Object;

            sut.Execute(null);

            service.Verify(x => x.BeginSearchBooks(request, It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }

        [TestMethod]
        public void can_call_BookReservation()
        {
            var request = new BookReservationRequest();

            var sut = CallService.For(request);
            sut.Service = service.Object;
			sut.SecurityContext = context.Object;

            sut.Execute(null);

            service.Verify(x => x.BeginBookReservation(request, It.IsAny<AsyncCallback>(), It.IsAny<object>()));
        }


        [TestMethod]
        [Asynchronous]
        public void can_call_the_real_service_and_get_a_response()
        {
            var realService = new SharedBookshelfServiceClient();
            var request = new LoginRequest();

            var sut = CallService.For(request);
            sut.Service = realService;
			sut.SecurityContext = context.Object;

            bool completed = false;
            sut.Completed += (o, e) =>
            {
                e.Error.Should().Be.Null();
                completed = true;
            };


            sut.Execute(null);

            EnqueueConditional(() => completed);
            EnqueueCallback(() =>
            {
                sut.Response.Should().Not.Be.Null();
                sut.Response.Should().Be.OfType<LoginResponse>();
            });

            EnqueueTestComplete();
        }


    }
}
